package com.onlyxiahui.of.muc.service;

import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.MultiUserChatManager;
import org.jivesoftware.openfire.muc.MultiUserChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;

import com.onlyxiahui.of.muc.dao.RoomDAO;

public class RoomService {

	private static final Logger log = LoggerFactory.getLogger(RoomService.class);

	public static void saveUser(JID roomJID, JID user, String nickname) {
		String userJid = user.toBareJID();
		String roomName = roomJID.getNode();
		boolean isexists = RoomDAO.exists(userJid, roomName);
		if (!RoomDAO.existsMember(roomName)) { // 如果muc房间里面一个用户都还不存在，说明房间未创建？
			return;
		}

		// if (RoomDAO.exists(userJid, roomName)) { // 如果成员存在
		// return;
		// } else if (!RoomDAO.existsMember(roomName)) { // 如果muc房间里面一个用户都还不存在，说明房间未创建？
		// return;
		// }
		log.warn("occupantJoined:" + roomJID + ">" + user + ":" + nickname);

		MultiUserChatManager mucm = XMPPServer.getInstance().getMultiUserChatManager();
		MultiUserChatService mucs = mucm.getMultiUserChatService(roomJID);
		if (null != mucs) {
			MUCRoom mucroom = mucs.getChatRoom(roomName);
			if (null != mucroom) {
				// IQ iq = new IQ(IQ.Type.set);
				// Element frag = iq.setChildElement("query",
				// "http://jabber.org/protocol/muc#member");
				// Element item = frag.addElement("item");
				// item.addAttribute("affiliation", "member");
				// item.addAttribute("jid", userJid);
				// item.addAttribute("nick", nickname);
				// Send the IQ packet that will modify the room's configuration
				try {

					long roomId = mucroom.getID();
					nickname = (null == nickname || nickname.isEmpty()) ? userJid : nickname;
					if (isexists) {
						String oldNickname = RoomDAO.getNickname(roomId, userJid);
						if (!nickname.equals(oldNickname)) {
							RoomDAO.updateNick(roomId, userJid, nickname);
						}
					} else {
						RoomDAO.saveMember(roomId, userJid, nickname);
					}

					// mucroom.getIQAdminHandler().handleIQ(iq, mucroom.getRole());
					// if (nickname != null && !"".equals(nickname)) {
					// RoomDAO.updateNick(roomId, userJid, nickname);
					// }
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e.getMessage(), e);
				}
			}
		}
	}

	public static void removeUser(JID roomJID, JID user) {
		String jid = user.toBareJID();
		String roomName = roomJID.getNode();
		RoomDAO.deleteUser(roomName, jid);
	}

	public static void updateNickname(JID roomJID, JID user, String oldNickname, String newNickname) {
		String userJid = user.toBareJID();
		String roomName = roomJID.getNode();
		MultiUserChatManager mucm = XMPPServer.getInstance().getMultiUserChatManager();
		MultiUserChatService mucs = mucm.getMultiUserChatService(roomJID);
		if (null != mucs) {
			MUCRoom mucroom = mucs.getChatRoom(roomName);
			if (null != mucroom) {
				try {
					long roomId = mucroom.getID();
					RoomDAO.updateNick(roomId, userJid, newNickname);
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e.getMessage(), e);
				}
			}
		}
	}
}
